package com.cy.ppj.service.banner.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.ppj.dao.mapper.banner.BannerMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.domian.banner.Banner;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.banner.BannerAdminQuery;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.banner.BannerService;


/**
 * BaseService
 *
 * @author: tanyilin
 * @create: 2018年9月20日 下午6:10:40
 */
@Service
public class BannerServiceImpl extends BaseService implements BannerService{

	@Autowired
	private BannerMapper bannerMapper;

	@Override
	@Transactional
	public void addBanner(Banner banner) {
		Date time = new Date();
		banner.setStatus(CommonStatus.ENABLE);
		banner.setCreateTime(time);
		banner.setUpdateTime(time);
		bannerMapper.insertSelective(banner);
	}

	@Override
	@Transactional
	public void updateBanner(Banner banner) {
		Date time = new Date();
		banner.setUpdateTime(time);
		bannerMapper.updateByPrimaryKeySelective(banner);
	}

	@Override
	public PageBean<Banner> selectBannerAdmin(BannerAdminQuery query) {
		int total = bannerMapper.selectBannerAdminCount(query);
		List<Banner> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = bannerMapper.selectBannerAdminList(query);
		}
		return new PageBean<>(list, total);
	}

	@Override
	public Banner selectBannerDetail(Long id) {
		Banner banner = bannerMapper.selectByPrimaryKey(id);
		return banner;
	}

	@Override
	public List<Banner> selectbannerList(Banner banner) {
		List<Banner> list = bannerMapper.select(banner);
		list.stream().forEach(b -> {
			b.setCreateTime(null);
			b.setUpdateTime(null);
			b.setSort(null);
			b.setStatus(null);
		});
		return list;
	}
	
	
}
